package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/regions")
@Controller
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ServletContext ctx;

    //根据状态{推荐}查询地区 用于找攻略页签显示
    @GetMapping("/{stateId}")
    @ResponseBody
    public Object listHot(@PathVariable Integer stateId){
        List<Region> list = regionService.listHot(stateId);
        return list;
    }

    @GetMapping
    @ResponseBody
    //APP记录我的旅游日记-----旅游地点下拉列表地区的查询
    public Object list(){
        List<Region> list = regionService.list();
        return list;

    }
    //地区下的所有攻略用于滑动返回JSON数据 拼接
    @GetMapping(value = "/{regionId}/strategies",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object getStrategiesByRegionIdJSON(StrategyQueryObject qo){
        System.out.println("JSON");
        return strategyService.getPageInfo(qo);
    }


    //导航页签数据的准备
    //由于接口的路径相同可以通过请求头进行接口访问的区分
    @GetMapping(value = "/{regionId}/strategies",produces = "text/html;charset=utf-8")
    public void getStrategiesByRegionIdHtml(StrategyQueryObject qo, HttpServletResponse resp)
            throws Exception {
        System.out.println("HTML");
        //查询该区域下的全部攻略(需要分页)
        qo.setPageSize(6);
        PageInfo alls = strategyService.getPageInfo(qo);
        //查询该区域下的推荐攻略(无需分页)
        qo.setPageSize(0);
        qo.setState(Strategy.STATE_HOT);
        PageInfo hot = strategyService.getPageInfo(qo);

        //通过Freemarker配置对象进行模板数据的渲染
        Configuration cf = new Configuration(Configuration.VERSION_2_3_28);
        cf.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/templates")));
        //获取模板文件
        Template template = cf.getTemplate("page.ftl","UTF-8");

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("alls",alls );
        map.put("hot",hot );

        template.process(map, resp.getWriter());
    }

}
